﻿using DataProcess.Core.DataValidation;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace DataProcess.Core.Model
{
    public class Equipment
    {
        public Equipment()
        {
            ID = Guid.NewGuid().ToString();
            Enabled = true;
            DataTransmissionConfigs = new List<DataTransmissionConfig>();
        }

        [Key]
        [Display(AutoGenerateField = false)]
        public string ID { get; set; }

        /// <summary>
        /// 设备名称
        /// </summary>
        [Display(Name = "设备名称")]
        [Required(ErrorMessage = "[设备名称]必须填写", AllowEmptyStrings = false)]
        public string Name { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [Display(Name = "是否启用")]
        public bool Enabled { get; set; }

        /// <summary>
        /// 自动运行
        /// </summary>
        [Display(Name = "自动运行")]
        public bool AutoRun { get; set; }

        /// <summary>
        /// 数据驱动文件名称
        /// </summary>
        [Display(Name = "数据驱动文件")]
        [Required(ErrorMessage = "[数据驱动文件]必须选择", AllowEmptyStrings = false)]
        public string DataDriveFile { get; set; }

        /// <summary>
        /// 数据是否需要经过处理
        /// </summary>
        [Display(Name = "数据需要处理")]
        public bool DataNeedProcess { get; set; }

        /// <summary>
        /// 数据处理文件名称
        /// </summary>
        [Display(Name = "数据处理文件")]
        [CustomValidation(typeof(EquipmentValidation), nameof(EquipmentValidation.DataProcessingFileValidation))]
        public string DataProcessingFile { get; set; }

        /// <summary>
        /// 数据驱动配置json字符串
        /// </summary>
        [Required(ErrorMessage = "[数据驱动配置]必须完整", AllowEmptyStrings = false)]
        public string DataDriveConfig { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Display(AutoGenerateField = false)]
        [JsonIgnore]
        public string DataTransmission
        {
            get
            {
                if (DataTransmissionConfigs == null) return string.Empty;
                return JsonConvert.SerializeObject(DataTransmissionConfigs);
            }
            set
            {
                try
                {
                    var result = JsonConvert.DeserializeObject<List<DataTransmissionConfig>>(value);
                    if (result == null)
                    {
                        DataTransmissionConfigs = new List<DataTransmissionConfig>();
                    }
                    else
                    {
                        DataTransmissionConfigs = result;
                    }
                }
                catch
                {
                    DataTransmissionConfigs = new List<DataTransmissionConfig>();
                }
            }
        }

        /// <summary>
        /// 数据传输配置
        /// </summary>        
        [SugarColumn(IsIgnore = true)]
        [Display(AutoGenerateField = false)]
        [CustomValidation(typeof(SystemSettingValidation), nameof(SystemSettingValidation.AnalysisItemsValidation))]
        public List<DataTransmissionConfig> DataTransmissionConfigs { get; set; }

    }
}
